<?php
class Cookie {
    public static function set($key, $value, $expire=null, $path = null) {
        $cookieLifeTime = Config::get('COOKIE_LIFETIME');
        if(0 != $expire) {
            if  ($expire > 0) {
                if ($expire < time()) {
                    $expire = time() + $expire;
                }
            } else if(-1 == $expire) {
                $expire = -1;
            }
        } else {
            if($cookieLifeTime)
                $expire = time() + $cookieLifeTime;
            else
                $expire = 0;
        }
        $path = null == $path ? Config::get('COOKIE_PATH') : $path;
        $cookieValue = Xxtea::encrypt(serialize($value), APP_KEY);
        setcookie($key, $cookieValue, $expire, $path, Config::get('COOKIE_DOMAIN'),false, Config::get('COOKIE_HTTPONLY'));
    }

    public static function get($key) {
        $cookieValue = $_COOKIE[$key];
        return unserialize(Xxtea::decrypt($cookieValue, APP_KEY));
    }

    public static function delete($key, $path = null) {
        $value = self::get($key);
        $path = null == $path ? Config::get('COOKIE_PATH') : $path;
        self::set($key, '', -1, $path);
        return $value;
    }
};
?>